package view.bullet
{

	import data.Globals;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import manager.EffectName;
	import manager.ObjectPool;
	
	import utils.GameUtils;
	import utils.HitTest;
	

	public class BaseBullet extends Sprite
	{
		
		protected var display:DisplayObject;
		
		public var onRemove:Function;
		
		private var _ART:int = 3;
		
		private var _speed:int;
		
		private var xSpeed:int;
		
		private var ySpeed:int;
		
		private var _tarPoint:Point;
		
		private var _explode:String;
		
		private var _isExplode:Boolean;
		
		private var _hurtRange:int;
		
		protected var monterAry:Array = Globals.gameView.getMonsterAry();
		
		protected var plantAry:Array = Globals.gameView.getPlantAry();
		
		public var shoutTarget:DisplayObject;
		
		protected var explode_x:int;
		
		protected var explode_y:int;
	
		public function BaseBullet()
		{
			super();
		}
		
		public function move():void{
			this.x += xSpeed;
			this.y += ySpeed;
			checkIsOutBorder();
		}
	
		private function checkIsOutBorder():void
		{
			if(this.x<0||this.x>Globals.stage_width||this.y<0||this.y>Globals.stage_height){
				remove();
			}
		}
		
		public function remove():void{
			if(this.parent){
				this.parent.removeChild(this);
				if(display is MovieClip){
					(display  as MovieClip).stop();
				}
				ObjectPool.putObj(this);
				onRemove(this);
			}
		}
		
		public function checkHitMonster():void{
			var i:int = 0;
			var lens:int = monterAry.length;
			for(i;i<lens;i++){
				var o:Object = HitTest.complexHitTestObject1(monterAry[i],this);
				if(o.hit){
					explode_x = o.rect.x+o.rect.width/2;
					explode_y = o.rect.y+o.rect.height/2;
					if(!this.isExplode){
						monterAry[i].blood -= ART;
						try{
							Globals.gameView.createExplode(this._explode,explode_x,explode_y);
						}catch(e:TypeError){
							
						}
					}
					else hurtRangObj(explode_x,explode_y);
					
					remove();
					return;
				}
			}
			i=0;
			lens = plantAry.length;
			for(i;i<lens;i++){
				var o:Object = HitTest.complexHitTestObject1(plantAry[i],this);
				if(o.hit){
					explode_x = o.rect.x+o.rect.width/2;
					explode_y = o.rect.y+o.rect.height/2;
					if(!this.isExplode){
						plantAry[i].blood -= ART;
						try{
							Globals.gameView.createExplode(this.explode,explode_x,explode_y);
						}catch(e:TypeError){
							
						}
					}else hurtRangObj(explode_x,explode_y);
					
					remove();
					return;
				}
			}
		}
		
		
		private function hurtRangObj(_x:int,_y:int):void
		{
			var monsterAry:Array = Globals.gameView.getMonsterAry();
			var i:int = 0;
			var lens:int
			for(i;i<monsterAry.length;i++){
				lens = GameUtils.getTwoPointDistance(new Point(_x,_y),new Point(monsterAry[i].x,monsterAry[i].y));
				if(lens<hurtRange){
					monsterAry[i].blood -= ART;
				}
			}
			monsterAry = Globals.gameView.getPlantAry();
			for(i;i<monsterAry.length;i++){
				lens = GameUtils.getTwoPointDistance(new Point(_x,_y),new Point(monsterAry[i].x,monsterAry[i].y));
				if(lens<hurtRange){
					monsterAry[i].blood -= ART;
				}
			}
			Globals.gameView.createExplode(EffectName.C_EXPLODE,_x,_y,0,0.3);
		}
		public function get speed():int
		{
			return _speed;
		}

		public function set speed(value:int):void
		{
			_speed = value;
			var p:Point = GameUtils.getSpeed(new Point(this.x,this.y),new Point(tarPoint.x,tarPoint.y),value);
			xSpeed = p.x;
			ySpeed = p.y;
		}

		/**
		 *攻击力 
		 */
		public function get ART():int
		{
			return _ART;
		}

		/**
		 * @private
		 */
		public function set ART(value:int):void
		{
			_ART = value;
		}

		/**
		 * 目标点坐标 
		 */
		public function get tarPoint():Point
		{
			return _tarPoint;
		}

		/**
		 * @private
		 */
		public function set tarPoint(value:Point):void
		{
			_tarPoint = value;
			this.rotation = GameUtils.getRotation(new Point(this.x,this.y),value);
		}
		
		public function setDisplay(posX:int,posY:int):void{
			if(display is MovieClip){
				(display as MovieClip).play();
			}
			if(posX != 0){
				display.x = posX;
			}
		    if(posY !=0){
				display.y = posY;
			}
		}

		/**
		 *爆炸特效 
		 */
		public function get explode():String
		{
			return _explode;
		}

		/**
		 * @private
		 */
		public function set explode(value:String):void
		{
			_explode = value;
		}

		/**
		 *是否是榴弹产生爆炸伤害 
		 */
		public function get isExplode():Boolean
		{
			return _isExplode;
		}

		/**
		 * @private
		 */
		public function set isExplode(value:Boolean):void
		{
			_isExplode = value;
		}

		/**
		 *杀伤范围 
		 */
		public function get hurtRange():int
		{
			return _hurtRange;
		}
		
		/**
		 * @private
		 */
		public function set hurtRange(value:int):void
		{
			_hurtRange = value;
			isExplode = true;
		}
	}
}